/**
 * Created by LinJe on 2015/9/22.
 * 全局ui：登陆提示 弹窗
 */
(function( $, window ) {

    window.globalLayer = window.globalLayer || {};


    /**
     * 调用实例
     * window.globalLayer.openComment({'callBack':function() {
            console.log('登陆提示窗已关闭');
       }});
     * @param settings
     */
    window.globalLayer.openExcerpt = function( settings ) {

        var excerptArgs;

        //如果是第一次调用，加载弹窗所需html和js变量
        if ( !this.excerptArgs ) {
            $('body').append('\
                <div class="layer_excerpt">\
                    <div class="popup-bg"></div>\
                    <div class="popup release-ugc" style="height:480px;margin-top:-240px;">\
                        <div class="content">\
                            <div class="title clearfix"><div class="text">添加摘抄</div><div class="btn"><span class="close" title="关闭"></span></div></div>\
                            <div class="from excerpt">\
                                <input name="" type="text" placeholder="与此商品有关的网址">\
                                <textarea name="" placeholder="你对此链接的描述，或者粘贴其中的节选"></textarea>\
                                <div class="text-tips">剩余500字</div>\
                                <div class="button"><button type="button" class="button-default button-disabled">发布</button></div>\
                            </div>\
                        </div>\
                    </div>\
                </div>\
            ');

            excerptArgs = this.excerptArgs = {};
            excerptArgs.$excerptWrap = $('.layer_excerpt');
            excerptArgs.$colse = excerptArgs.$excerptWrap.find('.title .btn');
            excerptArgs.$publish = excerptArgs.$excerptWrap.find('.button .button-default');
            excerptArgs.$textTips = excerptArgs.$excerptWrap.find('.text-tips');
            excerptArgs.$input = excerptArgs.$excerptWrap.find('.from input');
            excerptArgs.$textarea = excerptArgs.$excerptWrap.find('.from textarea');


            //输入框
            var txtAreaAllow = false;
            excerptArgs.$textarea.on('keydown', function() {
                //限制字数
                $.maxTextLen({
                    'field': excerptArgs.$textarea,
                    'countfield': excerptArgs.$textTips,
                    'callBack': function(data) {
                        //是否激活发布按钮
                        if ( data && excerptArgs.$input.val().length > 0 ) {
                            excerptArgs.$publish.removeClass('button-disabled');
                        } else {
                            excerptArgs.$publish.addClass('button-disabled');
                        }
                        txtAreaAllow = data;
                    }
                });
            });
            excerptArgs.$textarea.on('keyup', function() {
                //限制字数
                $.maxTextLen({
                    'field': excerptArgs.$textarea,
                    'countfield': excerptArgs.$textTips,
                    'callBack': function(data) {
                        //是否激活发布按钮
                        if ( data && excerptArgs.$input.val().length > 0 ) {
                            excerptArgs.$publish.removeClass('button-disabled');
                        } else {
                            excerptArgs.$publish.addClass('button-disabled');
                        }
                        txtAreaAllow = data;
                    }
                });
            });
            excerptArgs.$input.on('keydown', function() {
                //限制字数
                if ( txtAreaAllow && excerptArgs.$input.val().length > 0 ) {
                    excerptArgs.$publish.removeClass('button-disabled');
                } else {
                    excerptArgs.$publish.addClass('button-disabled');
                }
            });
            excerptArgs.$input.on('keyup', function() {
                //限制字数
                if ( txtAreaAllow && excerptArgs.$input.val().length > 0 ) {
                    excerptArgs.$publish.removeClass('button-disabled');
                } else {
                    excerptArgs.$publish.addClass('button-disabled');
                }
            });


            //点击取消或者X 关闭弹窗
            excerptArgs.$colse.on('click', function() {
                window.globalLayer.closeExcerpt(function(){
                    excerptArgs.callBack({'url':'', 'content':''});
                });
            });

            //点击 发布按钮
            excerptArgs.$publish.on('click', function() {
                if ( !$(this).hasClass('button-disabled') ) {
                    var url = excerptArgs.$input.val();
                    if ( !$.isUrl(url) ) {
                        window.globalLayer.openWarning({'autoClose':true, time: 1000, text:'网址格式不正确'});
                        return;
                    }
                    window.globalLayer.closeExcerpt(function(){
                        //回调
                        excerptArgs.callBack({'url':url, 'content':excerptArgs.$textarea.val()});
                        //还原
                        excerptArgs.$input.val('');
                        excerptArgs.$textarea.val('');
                        excerptArgs.$textTips.removeClass('red').text('剩余500字');
                        excerptArgs.$publish.addClass('button-disabled');
                    });
                }
            });
        }

        excerptArgs = this.excerptArgs;
        $.extend(excerptArgs, settings);

        excerptArgs.$excerptWrap.css({'opacity':'0', 'display':'block'}).stop().animate({ 'opacity': 1 });
    };


    window.globalLayer.closeExcerpt = function( callBack ) {

        this.excerptArgs.$excerptWrap.stop().animate({ 'opacity': 0 }, 500, '', function() {
            $(this).hide();
            callBack && callBack();
        });

    };

})( jQuery, window );